//
//  NewsPictureViewController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/22.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "NewsPictureViewController.h"

@implementation NewsPictureViewController



{
    UICollectionView* _Picture;
    NSMutableArray* _dataArray;
    UINavigationController* nav;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _loadData];
    [self creatCollectionView];
}





#pragma mark - 创建CollectionView
- (void)creatCollectionView
{
    
    
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kWidth-25)/4, 81);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    _Picture = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 0, kWidth-10, kHieght - 64) collectionViewLayout:layout];
    
    _Picture.delegate = self;
    _Picture.dataSource = self;
    
    [_Picture registerClass:[ImageCell class] forCellWithReuseIdentifier:@"PictureCell"];
    
    [self.view addSubview:_Picture];
    
}

#pragma mark - 定义子类Cell

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_dataArray count];
}


- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCell" forIndexPath:indexPath];
    
    
    cell.imageModal = _dataArray[indexPath.row];

    return cell;
}

#pragma mark - 点击图片切换大图模式
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
 
    PhotoView* photoV = [[PhotoView alloc] init];
    nav = [[UINavigationController alloc] initWithRootViewController:photoV];
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_all-64"] forBarMetrics:UIBarMetricsDefault];
    
    
    photoV.index = indexPath.row;
    
    photoV.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)];

    photoV.dataArray = [NSArray arrayWithArray:_dataArray];
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (void)cancelAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 数据加载
- (void)_loadData
{
    _dataArray = [[NSMutableArray alloc] init];
    NSArray* array = [DataService getArrayOrDictionNaryfromJosnWithNamed:@"image_list"];
    
    for(NSDictionary* dic in array)
    {
        ImageModal* modal = [[ImageModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_dataArray addObject:modal];
    }
    
}








@end

